package com.normalClasses.eye;

import java.util.ArrayList;
import java.util.LinkedList;
import freemarker.core.ParseException;


public class LoginBehavior {
  private ArrayList<Long> timeList;
  //private ArrayList<String> informationList;
  private final String[] symbols={"visited indexlogin interface","Landing successfully","perform login status modification"
      ,"preempt the same account being used","fail to login","new cookie state inserted"};
  private ArrayList<userBehavior> loginUserBehaviors;
  private double threshold=20;
  //private double upThreshold;
  //private double downThreshold;
  
  public LoginBehavior(){
    this.timeList=new ArrayList<Long>();
    //this.informationList=new ArrayList<String>();
    this.loginUserBehaviors=new ArrayList<userBehavior>();
  }
  public LoginBehavior(LinkedList<userBehavior> userBehaviors){
    this();
    this.updateLoginStatus(userBehaviors);
    }
  
 public LoginBehavior(LinkedList<userBehavior> userBehaviors,double threshold) {
   this();
   this.setThreshold(threshold);
   this.updateLoginStatus(userBehaviors);
   }
 
  public ArrayList<Long> getTimeList() {
    return this.timeList;
  }
  
  public void setThreshold(double threshold) {
    this.threshold=threshold;
  }
  
  //public ArrayList<String> getInformationList(){
    //return this.informationList;
  //}
  //过滤出跟登陆行为相关的log
  public void updateLoginStatus(LinkedList<userBehavior> userBehaviors){
    this.timeList=new ArrayList<Long>();
    this.loginUserBehaviors=new ArrayList<userBehavior>();
    for(int i=0;i<userBehaviors.size();i++){
      String information=userBehaviors.get(i).getInformation();
      for(int j=0;j<this.symbols.length;j++){
        if(information.contains(this.symbols[j])){
          //informationList.add(information);
          loginUserBehaviors.add(userBehaviors.get(i));
          timeList.add(Long.parseLong((userBehaviors.get(i).getTimeStamp())));
          break;
        }
      }
    }
  }
  
  private long getMin(){
    long min=timeList.get(0);
    for(int i=1;i<this.timeList.size();i++) {
      if (this.timeList.get(i)<min) {
        min=timeList.get(i);
        }
    }
    return min;
  }
  
  private long getMax(){
    Long max=timeList.get(0);
    for(int i=1;i<this.timeList.size();i++) {
      if (this.timeList.get(i)>max) {
        max=timeList.get(i);
        }
    }
    return max;
  }
  
 /* public double getIndex() {
    long min=this.getMin();
    long max=this.getMax();
    return(this.informationList.size()/(max-min));
    }
  
  public int getFailLogin() {
    int i=0;
    for(int j=0;j<loginUserBehaviors.size();i++) {
      if (loginUserBehaviors.get(j).getInformation().equals("fail to login")) {
        i++;
      }
    }
    return i;
  }*/
  //行为分析
  /**
   * 
   * @param seconds 规定的秒数，比如输入300秒，意味着当用户在300秒内出现与登陆相关的行为就会被判定为异常
   * @return 如果返回不是空字符串，那么就是有异常，如果返回是空，则没有异常
   * @throws java.text.ParseException
   */
  public String behaviorAnalysis(int seconds) throws java.text.ParseException{
    String information="";
    if(this.timeList.size()>0){
    int count=0;
    long start=this.getMin();
    //System.out.println(start);
    long end=this.getMax();
    //System.out.println(end);
    for(long i=start;i<end;i=i+1000*seconds){
      for(int j=0;j<this.loginUserBehaviors.size();j++){
        if(Long.parseLong(this.loginUserBehaviors.get(j).getTimeStamp())>=i&&Long.parseLong(this.loginUserBehaviors.get(j).getTimeStamp())<=i+1000*seconds){
          //System.out.println("加入");
          //System.out.println(loginUserBehaviors.get(j).getInformation()+dateToStamp.stampTDate(loginUserBehaviors.get(j).getTimeStamp()));
          count=count+1;
        }
     } 
      //int frequency=count/seconds;
      if(count>threshold){
        information=information+"The user has unusual login behavior between "+dateToStamp.stampTDate(String.valueOf(i))+" and "+dateToStamp.stampTDate(String.valueOf(i+1000*seconds))+"\n";
        //System.out.println("This user has unusual login behavior between"+dateToStamp.stampTDate(String.valueOf(i))+" and "+String.valueOf(i+1000*seconds));
      }
      count =0;
    }
    return information;
    }
    else{ 
      return information;
      }
  }  
}
